package com.hualude.roadsign.service.impl;

import com.hualude.roadsign.dto.*;
import com.hualude.roadsign.repository.SolarFaultRepository;
import com.hualude.roadsign.repository.UserRepository;
import com.hualude.roadsign.service.SolarEnergyHandleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by ding on 2017/7/12.
 */
@Service
public class SolarEnergyHandleServiceImpl implements SolarEnergyHandleService {
    @Resource
    private SolarFaultRepository solarFaultRepository;
    @Resource
    private UserRepository userRepository;

    public DTResponseDto getSolarHandle4DT(DTRequestDto dtRequestDto, String actorIds, Integer orgId,Integer solarMaintenanceStatus) throws Exception {
        List<DTSolarFaultDto> dto4DTs = solarFaultRepository.getSolarHandle4DT(dtRequestDto,actorIds, orgId,solarMaintenanceStatus);
        Integer count = solarFaultRepository.getSolarHandleCount4DT(dtRequestDto,actorIds, orgId,solarMaintenanceStatus);
        DTResponseDto dtResponseDto = new DTResponseDto(dtRequestDto.getDraw(), count, count, dto4DTs);
        for(DTSolarFaultDto dtSolarFaultDto:dto4DTs){
            if(dtSolarFaultDto.getSolarMaintainerId()!=null){
                String solarMaintainerName = userRepository.getUserNameByUserId(dtSolarFaultDto.getSolarMaintainerId());
                dtSolarFaultDto.setSolarMaintainerName(solarMaintainerName);
            }
        }
        return dtResponseDto;
    }

    @Transactional
    public Integer addSolarMaintenanceContent(Integer solarFaultId, String solarMaintenanceContent) throws Exception {
        return solarFaultRepository.addSolarMaintenanceContent(solarFaultId,solarMaintenanceContent);
    }

    public List<UserDto> getSolarMaintainer(Integer solarOrgId) throws Exception {
        return userRepository.getSolarMaintainer(solarOrgId);
    }

    @Transactional
    public Integer finishSolarMaintenance(String solarMaintenancePhoto, Integer solarMaintainerId, String solarMaintenanceTime, String solarMaintenanceExplain,Integer solarFaultId) throws Exception {
        return solarFaultRepository.finishSolarMaintenance(solarMaintenancePhoto,solarMaintainerId,solarMaintenanceTime,solarMaintenanceExplain,solarFaultId);
    }

    public List<SolarEnergyFaultDto> getSolarFaultHandleList4App(Integer orgId,Integer maintenanceStatus,String actorIds,Integer count,Integer size)throws Exception{
        return solarFaultRepository.getSolarFaultHandleList4App(orgId, maintenanceStatus, actorIds, count, size);
    }

    @Override
    public SolarEnergyFaultDto getReceiptInfo4App(Integer solarFaultId) throws Exception {
        return solarFaultRepository.getReceiptInfo4App(solarFaultId);
    }

}
